﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;
using System.Data.Common;

namespace Northwind.NET.EDM.FE.WinForms.Controls
{
    public partial class CustomersPhoneListUserControl : BaseUserControl
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            LoadCustomerContact();
        }

        private void LoadCustomerContact()
        {
            using (DbConnection connection = Factory.CreateConnection())
            {
                connection.ConnectionString = ConnectionString;
                DbDataAdapter adapter = Factory.CreateDataAdapter();
                DbCommand command = Factory.CreateCommand();
                command.Connection = connection;
                command.CommandText = "SELECT Name, ContactName, Phone, Fax FROM Customers";

                adapter.SelectCommand = command;

                DataSet dataset = new DataSet();
                dataset.Tables.Add("Customers");
                adapter.Fill(dataset.Tables["Customers"]);

                this.customerBindingSource.DataSource = dataset.Tables["Customers"];
            }
        }
        public CustomersPhoneListUserControl()
        {
            InitializeComponent();
        }

        public override string FormCaption { get { return Settings.Resources.CustomersPhoneListFormCaption; } }
        public override dynamic GetEntitySet(ObjectContext context)
        {
            return new Model.Customer();
        }

        public override BindingSource UserControlBindingSource { get { return customerBindingSource; } }

        private void letterbutton_Click(object sender, EventArgs e)
        {
            Button button = (Button)sender;

            string filter = button.Tag.ToString();
            if (filter != "*")
            {
                using (DbConnection connection = Factory.CreateConnection())
                {
                    connection.ConnectionString = ConnectionString;
                    DbDataAdapter adapter = Factory.CreateDataAdapter();
                    DbCommand command = Factory.CreateCommand();
                    command.Connection = connection;
                    command.CommandText = "SELECT ContactName FROM Customers WHERE ContactName LIKE '" + filter + "%'";

                    adapter.SelectCommand = command;

                    DataSet dataset = new DataSet();
                    dataset.Tables.Add("CustomerContact");
                    adapter.Fill(dataset.Tables["CustomerContact"]);

                    this.customerBindingSource.DataSource = dataset.Tables["CustomerContact"].AsEnumerable().Select(x => x.ItemArray[0]).ToList();
                }
                //Query Here.
                //var result = from p in _context.Customers
                //             where p.ContactName.StartsWith(filter)
                //             select p;
                //customerBindingSource.DataSource = result;
            }
            else
            {
                customerBindingSource.DataSource = new Model.Customer();
            }
        }

    }
}

